﻿using System.Collections.Generic;
using UnityEngine;

public class Main : MonoBehaviour
{
    private Rect OrignalRect;
    private GUIStyle orignalTxtStyle;

    [SerializeField]
    private List<Shape2D> shapeList;

    private int selectedIndex = -1;
    private Vector2 selectedOffset;
    private float selectedAngle;

    [SerializeField]
    private CollisionDetectionAlgorithmType collisionDetectionAlgorithmType = CollisionDetectionAlgorithmType.AABB;
    private CollisionDetectionBase collisionDetection;
    public CollisionDetectionBase CollisionDetection
    {
        get
        {
            if (collisionDetection == null)
            {
                switch (collisionDetectionAlgorithmType)
                {
                    case CollisionDetectionAlgorithmType.AABB:
                        collisionDetection = new CollisionDetectionAABB();
                        break;
                    case CollisionDetectionAlgorithmType.OBB_SAT:
                        collisionDetection = new SAT();
                        break;
                    case CollisionDetectionAlgorithmType.OBB_GJK:
                        collisionDetection = new CollisionDetectionGJK();
                        break;
                    default:
                        collisionDetection = new CollisionDetectionAABB();
                        break;
                }
            }
            return collisionDetection;
        }
    }

    public void Awake()
    {
        orignalTxtStyle = new GUIStyle();
        orignalTxtStyle.normal.textColor = Color.red;
        orignalTxtStyle.fontSize = 32;

        shapeList = new List<Shape2D>(gameObject.GetComponentsInChildren<Shape2D>());
    }
    public void Start()
    {
        Vector3 originalPos = Camera.main.WorldToScreenPoint(Vector3.zero);
        Debug.Log("width :" + Screen.width / 2 + " height :" + Screen.height / 2);
        Debug.Log(originalPos);
        OrignalRect = new Rect(originalPos.x + 5, Screen.height - originalPos.y + 5, 50, 50);
    }
    public void Update()
    {
        LineTool.Instance.BeginDraw();
        DrawAxis();
        DrawShape();
        UpdateSelection();
        CheckOverlap(shapeList);
        LineTool.Instance.EndDraw();
    }
    public void OnGUI()
    {
        GUI.Label(OrignalRect, "(0,0)", orignalTxtStyle);
    }
    /// <summary>
    /// 绘制坐标轴
    /// </summary>
    private void DrawAxis()
    {
        Color color = Color.white;
        float lineWidth = 0.02f;
        var renderer = LineTool.Instance.DrawLine(new Vector2(0, -10), new Vector2(0, 10), color);
        renderer.startWidth = lineWidth;
        renderer.endWidth = lineWidth;
        renderer = LineTool.Instance.DrawLine(new Vector2(-10, 0), new Vector2(10, 0), color);
        renderer.startWidth = lineWidth;
        renderer.endWidth = lineWidth;
    }
    /// <summary>
    /// 绘制所有图形
    /// </summary>
    private void DrawShape()
    {
        for (int i = 0; i < shapeList.Count; i++)
        {
            shapeList[i].DrawPolygon();
        }
    }
    /// <summary>
    /// 更新拖拽
    /// </summary>
    private void UpdateSelection()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition + new Vector3(0, 0, 10));
            selectedIndex = -1;
            for (int i = 0; i < shapeList.Count; ++i)
            {
                if (UtilityTool.contains(shapeList[i].vertices, mousePos))
                {
                    selectedIndex = i;
                    break;
                }
            }
            if (selectedIndex >= 0)
            {
                var t = shapeList[selectedIndex].transform;
                Vector2 position = t.position;
                selectedOffset = mousePos - position;
                float mouseAngle = Mathf.Atan2(selectedOffset.y, selectedOffset.x) * Mathf.Rad2Deg;
                selectedAngle = mouseAngle - t.eulerAngles.z;
            }
        }
        else if (Input.GetMouseButtonUp(0))
        {
            selectedIndex = -1;
        }
        else if (Input.GetMouseButton(0))
        {
            if (selectedIndex < 0)
            {
                return;
            }
            Transform t = shapeList[selectedIndex].transform;
            Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition + new Vector3(0, 0, 10));
            if (selectedOffset.magnitude < 0.4f) // 移动
            {
                Vector3 position = t.position;
                position.x = mousePos.x - selectedOffset.x;
                position.y = mousePos.y - selectedOffset.y;
                t.position = position;
            }
            else // 旋转
            {
                Vector2 position = t.position;
                Vector2 mouseDir = mousePos - position;
                float mouseAngle = Mathf.Atan2(mouseDir.y, mouseDir.x) * Mathf.Rad2Deg;

                float angle = mouseAngle - selectedAngle;
                t.eulerAngles = new Vector3(0, 0, angle);
            }
        }
    }
    /// <summary>
    /// 检测是否有重叠并改变图形颜色
    /// </summary>
    /// <param name="shapeList"></param>
    private void CheckOverlap(List<Shape2D> shapeList)
    {
        CollisionDetection.CheckOverlap(shapeList, selectedIndex);
    }
}

public enum CollisionDetectionAlgorithmType
{
    AABB,
    OBB_SAT,
    OBB_GJK
}